package cn.iocoder.yudao.module.reading.controller.app.follow;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.reading.controller.app.follow.vo.FollowUserRespVO;
import cn.iocoder.yudao.module.reading.service.follow.FollowService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 用户 APP - 关注系统
 *
 * @author 李晨
 */
@Tag(name = "用户 APP - 关注系统")
@RestController
@RequestMapping("/reading/follow")
@Validated
@Slf4j
public class FollowController {

    @Resource
    private FollowService followService;

    @PostMapping("/follow/{userId}")
    @Operation(summary = "关注用户")
    @PreAuthenticated
    public CommonResult<Long> followUser(
            @Parameter(description = "被关注用户ID", required = true, example = "1") @PathVariable("userId") Long userId) {
        Long followerId = getLoginUserId();
        Long followId = followService.followUser(followerId, userId);
        return success(followId);
    }

    @DeleteMapping("/unfollow/{userId}")
    @Operation(summary = "取消关注用户")
    @PreAuthenticated
    public CommonResult<Boolean> unfollowUser(
            @Parameter(description = "被关注用户ID", required = true, example = "1") @PathVariable("userId") Long userId) {
        Long followerId = getLoginUserId();
        followService.unfollowUser(followerId, userId);
        return success(true);
    }

    @PostMapping("/toggle/{userId}")
    @Operation(summary = "切换关注状态")
    @PreAuthenticated
    public CommonResult<Boolean> toggleFollow(
            @Parameter(description = "目标用户ID", required = true, example = "1") @PathVariable("userId") Long userId) {
        Long followerId = getLoginUserId();
        boolean isFollowing = followService.toggleFollow(followerId, userId);
        return success(isFollowing);
    }

    @PostMapping("/block/{userId}")
    @Operation(summary = "屏蔽用户")
    @PreAuthenticated
    public CommonResult<Boolean> blockUser(
            @Parameter(description = "被屏蔽用户ID", required = true, example = "1") @PathVariable("userId") Long userId) {
        Long followerId = getLoginUserId();
        followService.blockUser(followerId, userId);
        return success(true);
    }

    @DeleteMapping("/unblock/{userId}")
    @Operation(summary = "取消屏蔽用户")
    @PreAuthenticated
    public CommonResult<Boolean> unblockUser(
            @Parameter(description = "被屏蔽用户ID", required = true, example = "1") @PathVariable("userId") Long userId) {
        Long followerId = getLoginUserId();
        followService.unblockUser(followerId, userId);
        return success(true);
    }

    @GetMapping("/check/{userId}")
    @Operation(summary = "检查关注状态")
    @PreAuthenticated
    public CommonResult<Boolean> checkFollowStatus(
            @Parameter(description = "目标用户ID", required = true, example = "1") @PathVariable("userId") Long userId) {
        Long followerId = getLoginUserId();
        boolean isFollowing = followService.isFollowing(followerId, userId);
        return success(isFollowing);
    }

    @PostMapping("/batch-check")
    @Operation(summary = "批量检查关注状态")
    @PreAuthenticated
    public CommonResult<Map<Long, Boolean>> batchCheckFollowStatus(
            @Parameter(description = "用户ID列表", required = true) @RequestBody List<Long> userIds) {
        Long followerId = getLoginUserId();
        Map<Long, Boolean> result = followService.batchCheckFollowStatus(followerId, userIds);
        return success(result);
    }

    @GetMapping("/following")
    @Operation(summary = "获取关注列表")
    @PreAuthenticated
    public CommonResult<List<FollowUserRespVO>> getFollowingList(
            @Parameter(description = "限制数量", example = "20") @RequestParam(value = "limit", required = false) Integer limit) {
        Long userId = getLoginUserId();
        List<FollowUserRespVO> result = followService.getFollowingList(userId, limit);
        return success(result);
    }

    @GetMapping("/followers")
    @Operation(summary = "获取粉丝列表")
    @PreAuthenticated
    public CommonResult<List<FollowUserRespVO>> getFollowerList(
            @Parameter(description = "限制数量", example = "20") @RequestParam(value = "limit", required = false) Integer limit) {
        Long userId = getLoginUserId();
        List<FollowUserRespVO> result = followService.getFollowerList(userId, limit);
        return success(result);
    }

    @GetMapping("/mutual")
    @Operation(summary = "获取互相关注列表")
    @PreAuthenticated
    public CommonResult<List<FollowUserRespVO>> getMutualFollowList(
            @Parameter(description = "限制数量", example = "20") @RequestParam(value = "limit", required = false) Integer limit) {
        Long userId = getLoginUserId();
        List<FollowUserRespVO> result = followService.getMutualFollowList(userId, limit);
        return success(result);
    }

    @GetMapping("/stats")
    @Operation(summary = "获取关注统计信息")
    @PreAuthenticated
    public CommonResult<FollowService.FollowStatsVO> getFollowStats() {
        Long userId = getLoginUserId();
        FollowService.FollowStatsVO result = followService.getFollowStats(userId);
        return success(result);
    }

    @GetMapping("/stats/{userId}")
    @Operation(summary = "获取指定用户的关注统计信息")
    public CommonResult<FollowService.FollowStatsVO> getUserFollowStats(
            @Parameter(description = "用户ID", required = true, example = "1") @PathVariable("userId") Long userId) {
        FollowService.FollowStatsVO result = followService.getFollowStats(userId);
        return success(result);
    }

}
